package com.cwt_taxi_xa;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.GPSUpdataUtil;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.TTsFunction;
import com.cwt_taxi_xa.util.Tools;

import java.text.SimpleDateFormat;
import java.util.Date;
/**
 *
 * 鉴权界面
 * @author ferch
 *
 *
 */
public class ValidationPassWord extends CWTBaseActivity implements OnClickListener{
	public Button num0,num1,num2,num3,num4,num5,num6,num7,num8,num9,clear,delect,cancel,confirm;
	public EditText password;
	String mDate;
	int year;
	int month;
	int day;
	String psd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.validation);
		mDate = new SimpleDateFormat("yyMMdd").format(new Date());
		year = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
		month = Integer.parseInt(new SimpleDateFormat("MM").format(new Date()));
		day = Integer.parseInt(new SimpleDateFormat("dd").format(new Date()));
		double a = Double.parseDouble(Tools.muilt(mDate,mDate));
		psd = String.valueOf(Math.round(a / year / month / day * 10000)).substring(1,7);
		AppLog.e("psd= " + psd);
		AppLog.savelog("新的设备维护密码-----------" + psd);
		num0 = (Button)findViewById(R.id.num0);
		num1 = (Button)findViewById(R.id.num1);
		num2 = (Button)findViewById(R.id.num2);
		num3 = (Button)findViewById(R.id.num3);
		num4 = (Button)findViewById(R.id.num4);
		num5 = (Button)findViewById(R.id.num5);

		num6 = (Button)findViewById(R.id.num6);
		num7 = (Button)findViewById(R.id.num7);
		num8 = (Button)findViewById(R.id.num8);
		num9 = (Button)findViewById(R.id.num9);
		clear = (Button)findViewById(R.id.clear);
		delect = (Button)findViewById(R.id.delect);
		confirm = (Button)findViewById(R.id.confirm);
		cancel = (Button)findViewById(R.id.cancel);
		password = (EditText)findViewById(R.id.password);
		num0.setOnClickListener(this);
		num1.setOnClickListener(this);
		num2.setOnClickListener(this);
		num3.setOnClickListener(this);
		num4.setOnClickListener(this);
		num5.setOnClickListener(this);
		num6.setOnClickListener(this);
		num7.setOnClickListener(this);
		num8.setOnClickListener(this);
		num9.setOnClickListener(this);
		clear.setOnClickListener(this);
		confirm.setOnClickListener(this);
		delect.setOnClickListener(this);
		cancel.setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onClick(View view) {
		String passwdStr = password.getText().toString();
		switch (view.getId()) {
			case R.id.num0:
				password.setText(password.getText()+"0");
				break;
			case R.id.num1:
				password.setText(password.getText()+"1");
				break;
			case R.id.num2:
				password.setText(password.getText()+"2");
				break;
			case R.id.num3:
				password.setText(password.getText()+"3");
				break;
			case R.id.num4:
				password.setText(password.getText()+"4");
				break;
			case R.id.num5:
				password.setText(password.getText()+"5");
				break;
			case R.id.num6:
				password.setText(password.getText()+"6");
				break;
			case R.id.num7:
				password.setText(password.getText()+"7");
				break;
			case R.id.num8:
				password.setText(password.getText()+"8");
				break;
			case R.id.num9:
				password.setText(password.getText()+"9");
				break;

			case R.id.clear:
				password.setText("");
				password.setHint("请输入鉴权码");
				break;
			case R.id.confirm:

				if(passwdStr!=null&&!passwdStr.equals("")&&
						passwdStr.equals(new SimpleDateFormat("HHddMMyy").format(new Date()))){
				//toSetting();
			}

			if(passwdStr!=null&&passwdStr.equals(psd)){
					toSetting();
				}

//			passwdStr.equals(Constants.VALIDATION_PASSWD)){
//				toSetting();
//			}

				else if(passwdStr!=null&&!passwdStr.equals("")&&
						passwdStr.equals("1001")){
					MCUUtil.hasVic(true);
					Toast.makeText(ValidationPassWord.this, "功放测试", 2000).show();
					TTsFunction.speakTextDelay("功放已打开", 100);
				}

				else if(passwdStr!=null&&!passwdStr.equals("")&&
						passwdStr.equals("1002")){
					MainApplication.topActivity.startActivity(new Intent(MainApplication.topActivity, ActivitySPTest.class));
				}

			else if(passwdStr!=null&&!passwdStr.equals("")&&passwdStr.equals("1003")){
				Constants.PORT_PROTECT = true;
				SPUtils.setParam("xaiplock", true);
				Toast.makeText(ValidationPassWord.this, "锁定IP", Toast.LENGTH_LONG).show();
			}

			else if(passwdStr!=null&&!passwdStr.equals("")&&passwdStr.equals("1004")){
				Constants.PORT_PROTECT = false;
				SPUtils.setParam("xaiplock", false);
				Toast.makeText(ValidationPassWord.this, "解除IP锁定", Toast.LENGTH_LONG).show();
			}

			else if(passwdStr!=null&&!passwdStr.equals("")&&passwdStr.equals("666")){
				try {
					final GPSUpdataUtil util = new GPSUpdataUtil();
					util.initGpsUpdata(new GPSUpdataUtil.onGPSUpdata() {
						@Override
						public void onInit() {
							AppLog.e("GPS升级封装测试-onInit");
							util.check();
						}

						@Override
						public void onUpdata(int percent) {
							AppLog.e("升级封装测试-onUpdata："+percent);
						}

						@Override
						public void onSucceed() {
							SPUtils.setParam("gpsupdata",true);
							AppLog.e("GPS升级封装测试-onSucceed");
						}

						@Override
						public void onCheck(String version) {
							version = version.replace("V","");
							version = version.replace(".","");
							int vi = Integer.parseInt(version);
							AppLog.e("GPS升级封装测试-v:"+vi);
//								if(vi<=5300){
							if(vi<5300){
								AppLog.e("GPS升级封装测试-版本低于5300，开始升级:");
								MainApplication.topActivity.myToast("GPS模块"+vi+"  开始升级");
								util.upDate();
							}else{
								MainApplication.topActivity.myToast("GPS模块已升级新版本5300");
								AppLog.e("GPS升级封装测试-新版本，取消升级");
								util.restartAndclose();
							}
						}

						@Override
						public void onFailure() {
							SPUtils.setParam("gpsupdata",true);
							TTsFunction.speakText("GPS升级重启");
							try {
								Intent intent = new Intent(
										"android.intent.action.ACTION_REQUEST_SHUTDOWN");
								intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
								// 其中false换成true,会弹出是否关机的确认窗口
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								MainApplication.getInstance().startActivity(intent);

							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//			passwdStr.equals(Constants.VALIDATION_PASSWD2)){
//				toWriteCard();
//			}

//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals(Constants.VALIDATION_PASSWD3)){
//				SPConstants.log_test = !SPConstants.log_test;
//				if(SPConstants.log_test){
//					Toast.makeText(ValidationPassWord.this, "Applog日志已打开", 1500).show();
//				}else{
//					Toast.makeText(ValidationPassWord.this, "Applog日志已关闭", 1500).show();
//				}
//			}

//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals(Constants.VALIDATION_PASSWD4)){
//				int dvr = SPUtils.getParamInteger(SPConstants.SPK_dvr, SPConstants.SPV_dvr);
//				if(dvr==1){
//					Toast.makeText(ValidationPassWord.this, "启用自己家dvr", 1500).show();
//					SPUtils.setParam(SPConstants.SPK_dvr, 2);
//				}else{
//					Toast.makeText(ValidationPassWord.this, "启用通立dvr", 1500).show();
//					SPUtils.setParam(SPConstants.SPK_dvr, 1);
//				}
//			}

//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.startsWith("9999")){
//				DataUtil.savelastCarNum(passwdStr.substring(4));
//					Toast.makeText(ValidationPassWord.this, "强制卡号："+DataUtil.getlastCarNum(), 1500).show();
//			}
//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals("90752001")){
//				SPUtils.setParam("pingjiaqiback", true);
//				Toast.makeText(ValidationPassWord.this, "打开后排评价器", 1500).show();
//			}
//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals("90752002")){
//				SPUtils.setParam("pingjiaqiback", false);
//				Toast.makeText(ValidationPassWord.this, "禁用后排评价器", 1500).show();
//			}


//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals("13824123168")){
//				MainApplication.topActivity.startActivity(new Intent(MainApplication.topActivity, VideoActivity2.class));
//			}

//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals("2")){
//				MCUUtil.UpV();
//			}
//
//			else if(passwdStr!=null&&!passwdStr.equals("")&&
//					passwdStr.equals("3")){
//				MCUUtil.UpV2();
//			}


				else{
					password.setText("");
					password.setHint("密码无效");
				}
				break;
			case R.id.delect:
				if(passwdStr.length()>0){
					password.setText(passwdStr.substring(0, passwdStr.length()-1));
				}
				break;
			case R.id.cancel:
				ValidationPassWord.this.finish();
				break;

			default:
				break;
		}
	}

	public void toSetting(){

		Intent intent = new Intent();
		intent.setClass(ValidationPassWord.this, Setting.class);
		startActivity(intent);
		this.finish();
	}

	public void toWriteCard(){

		Intent intent = new Intent();
		intent.setClass(ValidationPassWord.this, ActivityWriteCard.class);
		startActivity(intent);
		this.finish();
	}


	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
